<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="会员等级" prop="memberCardName">
        <el-select v-model="queryParams.memberCardTypeId" clearable style="width: 240px">
          <el-option v-for="(m,index) in memberLevelList" :key="index" :label="m.name" :value="m.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员姓名" prop="orderName">
        <el-input
          v-model="queryParams.orderName"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="orderTelno">
        <el-input
          v-model="queryParams.orderTelno"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" clearable style="width: 240px" @change="handleQuery">
          <el-option label="扫码" value="0" />
          <el-option label="现金" value="1" />
          <el-option label="余额" value="2" />
          <el-option label="银联" value="3" />
          <el-option label="支付宝" value="4" />
          <el-option label="微信" value="5" />
          <el-option label="其他" value="6" />
        </el-select>
      </el-form-item>
      <el-form-item label="预定时间" prop="payTimeRange">
        <el-date-picker
          v-model="payTimeArr"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场地消费时间" prop="signTimeRange">
        <el-date-picker
          v-model="signTimeArr"
          type="datetimerange" value-format="YYYY-MM-DD"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="订单来源" prop="dataFrom">
        <el-select v-model="queryParams.dataFrom" clearable style="width: 240px" @change="handleQuery">
          <el-option label="小程序" value="0" />
          <el-option label="后台" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="预订类型" prop="orderType">
        <el-select v-model="queryParams.orderType" clearable style="width: 240px" @change="handleQuery">
          <el-option label="普通预定" value="0" />
          <el-option label="长租预定" value="1" />
          <el-option label="预留" value="2" />
          <el-option label="锁场" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否支付" prop="payStatus">
        <el-select v-model="queryParams.payStatus" clearable style="width: 240px" @change="handleQuery">
          <el-option label="未支付" value="0" />
          <el-option label="已支付" value="1" />
          <el-option label="部分退款" value="2" />
          <el-option label="已退款" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
            v-model="queryParams.remark"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="20">
        <el-button
          type="warning"
          plain @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>
    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      row-key="id" :data="tableData" :tree-props="{ children: 'sonList',hasChildren: true }"
      center
    >
      <!-- <el-table-column type="index" /> -->
      <el-table-column label="订单编号" prop="orderCode" min-width="240" />
      <el-table-column label="会员姓名" prop="orderName" min-width="100" />
      <el-table-column label="会员卡号" prop="memberCardCode" min-width="200" />
      <el-table-column label="手机号" prop="orderTelno" min-width="120" />
      <el-table-column label="卡片等级" prop="memberCardName" min-width="120" />
      <el-table-column label="预定时间" prop="orderDate" min-width="120" />
      <el-table-column label="预定类型" prop="orderTypeStr" min-width="120" />
      <el-table-column label="订场时长" prop="orderHour" min-width="100">
        <template #default="scope">
          {{ scope.row.orderHour }}h
        </template>
      </el-table-column>
      <el-table-column label="订场详情" min-width="160">
        <template #default="scope">
          <span>{{ scope.row.siteName ? scope.row.siteName +' '+ scope.row.orderTimePeriod.replace(/,/,'~') : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" prop="orderAmount" min-width="120" />
      <el-table-column label="优惠金额" width="120">
        <template #default="scope">
          <span>{{ decimalTo2(scope.row.orderAmount-scope.row.finalOrderAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="折后金额" prop="finalOrderAmount" min-width="120" />
      <el-table-column label="退款金额" prop="refundAmount" min-width="120" />
      <el-table-column label="订单来源" prop="dataFromStr" min-width="120" />
      <el-table-column label="支付方式" prop="payTypeStr" min-width="120" />
      <el-table-column label="支付状态" prop="payStatusStr" min-width="120" />
      <el-table-column label="场地消费时间" prop="signTime" min-width="120" />
      <el-table-column label="备注" prop="remark" min-width="120" />
      <el-table-column label="操作员" prop="operatorName" min-width="120" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding">
        <template #default="scope">
          <el-button type="text" @click="detailRow(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
    <!-- 详情 -->
    <el-dialog v-model="open" :title="title" width="800px" append-to-body>
      <el-descriptions :column="2">
        <el-descriptions-item label="订单编号">{{ rowDetail.orderCode }}</el-descriptions-item>
        <el-descriptions-item label="会员姓名">{{ rowDetail.orderName }}</el-descriptions-item>
        <el-descriptions-item label="会员卡号">{{ rowDetail.memberCardCode }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ rowDetail.orderTelno }}</el-descriptions-item>
        <el-descriptions-item label="卡片等级">{{ rowDetail.memberCardName }}</el-descriptions-item>
        <el-descriptions-item label="订场时长">{{ rowDetail.orderHour }}</el-descriptions-item>
        <el-descriptions-item label="订场详情">
          <span>{{ rowDetail.siteName ? rowDetail.siteName +' '+ rowDetail.orderTimePeriod.replace(/,/,'~') : '-' }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="订单金额">{{ rowDetail.orderAmount }}</el-descriptions-item>
        <el-descriptions-item label="优惠金额">
          <span>{{ decimalTo2(rowDetail.orderAmount-rowDetail.finalOrderAmount) }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="折后金额">{{ rowDetail.finalOrderAmount }}</el-descriptions-item>
        <el-descriptions-item label="支付方式">{{ rowDetail.payTypeStr }}</el-descriptions-item>
        <el-descriptions-item label="订单来源">{{ rowDetail.dataFromStr }}</el-descriptions-item>
        <el-descriptions-item label="支付状态">{{ rowDetail.payStatusStr }}</el-descriptions-item>
        <el-descriptions-item label="操作员">{{ rowDetail.operatorName }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="ven_order_venue">
import { ref, reactive } from 'vue'
import { getCurrentInstance, onMounted } from '@vue/runtime-core'
const { proxy } = getCurrentInstance()
import { decimalTo2 } from '@/utils/ruoyi'
import { getOrderList } from '@/api/ven/reserve'
import { memberLevel } from '@/api/ven/index'
const showSearch = ref(true)
const payTimeArr = ref([])
const signTimeArr = ref([])
const tableData = ref([])
const memberLevelList = ref([])
const total = ref(0)
const title = ref('')
const open = ref(false)
const rowDetail = ref({})
const loading = ref(true)
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    isParent: '1'
  },
  rules: {

  }
})
const { queryParams, form, rules } = toRefs(data)
// 获取列表
function getList() {
  loading.value = true
  if (payTimeArr.value) {
    queryParams.value.payTimeRange = payTimeArr.value.toString()
  }
  if (signTimeArr.value) {
    queryParams.value.signTimeRange = signTimeArr.value.toString()
  }
  getOrderList(queryParams.value).then(res => {
    loading.value = false
    tableData.value = res.rows
    total.value = res.total
  })
}
function handleExport() {
  proxy.download('/systemj/siteOrder/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  payTimeArr.value = []
  handleQuery()
}
function getMemberLevel() {
  memberLevel().then(res => {
    memberLevelList.value = res
  })
}

function detailRow(row) {
  rowDetail.value = row
  open.value = true
  title.value = '订单详情'
}

getList()
getMemberLevel()
</script>

<style scoped>

</style>
